package com.cainiao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author shkstart
 * @create 2021-08-05 16:50
 */
//向 request域中设置数据
@Controller
public class ScopeController {

    /*
     * Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
     * org.springframework.validation.support.BindingAwareModelMap
     *
     * public class BindingAwareModelMap extends ExtendedModelMap
     * public class ExtendedModelMap extends ModelMap implements Model
     * public class ModelMap extends LinkedHashMap<String, Object>
     * */

    /**
     * 使用 servletAPI向 request域对象共享数据
     *
     * @return
     */
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request) {
        request.setAttribute("testRequestCope", "hello,servlet");
        return "success";
    }

    /**
     * 使用 ModelAndView 设置共享数据**返回值必须是 modelandview
     * **
     * 一共两个功能
     * 1. 向 request 域中设置共享数据
     * 2. 设置视图名称
     *
     * @return
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView test() {
        ModelAndView view = new ModelAndView();
        //处理模型数据，即向请求域 request共享数据
        view.addObject("testRequestCope", "hello,ModelAndView");
        //设置视图名称
        view.setViewName("success");
        return view;
    }

    /**
     * model
     *
     * @param model
     * @return
     */
    @RequestMapping("/testModel")
    public String testModel(Model model) {
        model.addAttribute("testRequestCope", "hello,model");
        System.out.println(model.getClass().getName());
        return "success";
    }

    /**
     * 使用 map向 request域中传入数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/testMap")
    public String testMap(Map<String, Object> map) {
        map.put("testRequestCope", "hello,Map");
        System.out.println(map.getClass().getName());
        return "success";
    }

    /**
     * ModelMap
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap) {
        modelMap.addAttribute("testRequestCope", "hello,modelMap");
        System.out.println(modelMap.getClass().getName());
        return "success";
    }


    /**
     * 向 session域中共享数据
     *
     * @param session
     * @return
     */
    @RequestMapping("/testSession")
    public String testSession(HttpSession session) {
        session.setAttribute("testSessionScope", "hello,Session");
        return "success";
    }

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session) {
        ServletContext aplication = session.getServletContext();
        aplication.setAttribute("testApplicationScope", "hello,Application");
        return "success";
    }

}
